package com.yulebaby.callsenter.task;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.lzy.okgo.model.HttpParams;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.HttpHelper;
import com.yulebaby.callsenter.http.ServerUrls;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.ui.activity.LoginActivity;
import com.yulebaby.callsenter.utils.SPUtil;
import com.yulebaby.callsenter.utils.ToastUtil;

/**
 * Author: Zhangbo
 * Time: 2017/1/21
 */
public class RequestTranProgressTask {

    /**
     * 查询实际转换进度
     *
     * @param activity 获取信息的Acitivty
     */
    public static void doRequestActualTranProgress(Activity activity, HttpCallback callback) {
        HttpHelper.getInstance().get(activity, ServerUrls.OPT_FINDACTUALTRANPROGRESS, getHttpParams(activity), callback);
    }

    /**
     * 查询跟踪目标是否存在
     *
     * @param activity 获取信息的Acitivty
     */
    public static void doRequestCheckTrackTarget(Activity activity, HttpCallback callback) {
        HttpHelper.getInstance().get(activity, ServerUrls.OPT_CHECKTRACKTARGET, getHttpParams(activity), callback);
    }


    /**
     * 查询当日动态日报
     *
     * @param activity 获取信息的Acitivty
     */
    public static void doRequestDynamicDaily(Activity activity, String time, HttpCallback callback) {
        if (TextUtils.isEmpty(time)) {
            HttpHelper.getInstance().get(activity, ServerUrls.OPT_FINDSALEDAILYTODAY, getHttpParams(activity), callback);
        } else {
            HttpParams httpParams = getHttpParams(activity);
            httpParams.put("dateTime", time);
            HttpHelper.getInstance().get(activity, ServerUrls.OPT_FINDSALEDAILYDETAIL, httpParams, callback);
        }
    }

    /**
     * 查询售前跟踪记录
     *
     * @param activity 获取信息的Acitivty
     */
    public static void doRequestFindPreTrackToday(Activity activity, int traPro, HttpCallback callback) {
        HttpParams httpParams = getHttpParams(activity);
        httpParams.put("tra_pro", traPro);
        HttpHelper.getInstance().get(activity, ServerUrls.OPT_FINDPRETRACKTODAY, httpParams, callback);
    }


    /**
     * 设置一个总的HttpParams
     *
     * @return
     */
    static private HttpParams getHttpParams(Activity activity) {
        HttpParams httpParams = new HttpParams();
        String user_token = (String) SPUtil.get(Constant.USER_TOKEN, "");
        int user_id = (int) SPUtil.get(Constant.USER_ID, -1);
        httpParams.put("token", user_token);
        httpParams.put("sale_id", user_id);
        checkNull(user_token, activity);
        return httpParams;
    }

    /**
     * 判断是否为空
     *
     * @param string
     * @return
     */
    private static boolean checkNull(String string, Activity mActivity) {
        if (TextUtils.isEmpty(string)) {
            ToastUtil.showToast("登录信息出错,请重新登录");
            //TOKEN 失效跳转到登录界面
            SPUtil.remove(Constant.USER_TOKEN); //对登录信息进行清除
            SPUtil.remove(Constant.USER_ID);
            if (mActivity != null) {
                mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                mActivity.finish();
            }
            return false;
        }
        return true;
    }
}
